#include "uartHandle.h"
#include "uart.h"
#include "protocol.h"

static uint16_t rIndex = 0;
static uint16_t wIndex = 0;
static uint16_t rIndexMircobit = 0;
static uint16_t wIndexMircobit = 0;

static void uartDataHandle(uint8_t data) 
{
    static uint8_t status;
    static uint8_t cksum;
    static uint8_t len;
    static uint8_t bufIndex;
    static uint8_t cmd;
    static uint8_t buf[30];
    switch (status)
	{
		case 0:
			status = (data == 0xFD) ? 1 : 0;
			break;
		case 1:
			status = (data == 0xFD) ? 2 : 0;
			break;
		case 2:
			len = data;
			bufIndex = 0;
			cksum = data;
			status = 3;
			break;
		case 3:
			cmd = data;
			bufIndex = 0;
			cksum += data;
			if (len != 0)
			{
				status = 4;
			}
			else
			{
				status = 5;
			}
			break;
		case 4: 
			buf[bufIndex] = data;
			bufIndex++;
			cksum += data;
			if (bufIndex == len)
			{	
				/***
				最后一个字节在这里收到的，
				避免收完数据后不在进入这个函数,
				所以直接把处理的放到接收最后一个数据之后处理
				**/
				//if (cksum == data) /*所有校验正确*/
				{	
					bl602DataHandle(cmd, buf);
				}	
				status = 0;
			}
			break;//
		default:
			break;
    }
}

static void uartDataHandleMircobit(uint8_t data) 
{
    static uint8_t status;
    static uint8_t cksum;
    static uint8_t len;
    static uint8_t bufIndex;
    static uint8_t cmd;
    static uint8_t buf[30];
    switch (status)
	{
		case 0:
			status = (data == 0xFD) ? 1 : 0;
			break;
		case 1:
			status = (data == 0xFD) ? 2 : 0;
			break;
		case 2:
			len = data;
			bufIndex = 0;
			cksum = data;
			status = 3;
			break;
		case 3:
			cmd = data;
			bufIndex = 0;
			cksum += data;
			if (len != 0)
			{
				status = 4;
			}
			else
			{
				status = 5;
			}
			break;
		case 4: 
			buf[bufIndex] = data;
			bufIndex++;
			cksum += data;
			if (bufIndex == len)
			{	
				/***
				最后一个字节在这里收到的，
				避免收完数据后不在进入这个函数,
				所以直接把处理的放到接收最后一个数据之后处理
				**/
				//if (cksum == data) /*所有校验正确*/
				{	
					hc32f4dataHandle(cmd, buf);
				}	
				status = 0;
			}
			break;//
		default:
			break;
    }
}

void uartUpdate(void)
{
    wIndex = getRemainRecvBuf();
    if (rIndex != wIndex)
    {
        int16_t len = wIndex - rIndex;
        if (len < 0)
        {
            len += 256;
        }
        uint16_t i = 0;
        while (i < len)
        {
            uint16_t index = rIndex + i;
            i++;
            if (index > 255)
            {
                index -= 256;
            }
            uartDataHandle(g_rx[index]);
        }
        rIndex = wIndex;
    }
}

void uartUpdateMicrobit(void)
{ 	
    wIndexMircobit = getRemainRecvBuf_mircobit();
    if (rIndexMircobit != wIndexMircobit)
    {
        int16_t len = wIndexMircobit - rIndexMircobit;
        if (len < 0)
        {
            len += 256;
        }
        uint16_t i = 0;
        while (i < len)
        {
            uint16_t index = rIndexMircobit + i;
            i++;
            if (index > 255)
            {
                index -= 256;
            }
            uartDataHandleMircobit(g_rx_mircobit[index]);
        }
        rIndexMircobit = wIndexMircobit;
    }
}


